package com.lishuang.base;

/**
 * @author LiShuang
 * @version 1.0.0
 * @date 2022/11/5 21:55
 */


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

public class Load_ {

    OperatingSystemMXBean operatingSystemMXBean;
    String osJson;
    JSONObject jsonObject;

    public Load_(){
        operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        osJson = JSON.toJSONString(operatingSystemMXBean);
        jsonObject = JSON.parseObject(osJson);
    }

    public float getCPULoad(){return jsonObject.getFloatValue("systemCpuLoad") * 100;}

    public float getMemoryLoad(){
        final long GB = 1024 * 1024 * 1024;
        Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
        Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
        double totalMemory = 1.0 * totalPhysicalMemorySize / GB;
        double freeMemory = 1.0 * freePhysicalMemorySize / GB;
        float memoryUseRatio = (float) (1.0 * (totalPhysicalMemorySize - freePhysicalMemorySize) / totalPhysicalMemorySize * 100);
        return memoryUseRatio;
    }

    public String getLoad(){
        return "当前CPU使用率："+getCPULoad()+"%, 当前内存使用率："+getMemoryLoad()+"%.";
    }
//      参考代码
//    public void testSystemUsage() {
//        final long GB = 1024 * 1024 * 1024;
//        while (true) {
//            JSONObject jsonObject = JSON.parseObject(osJson);
//            double processCpuLoad = jsonObject.getDouble("processCpuLoad") * 100;
//            double systemCpuLoad = jsonObject.getDouble("systemCpuLoad") * 100;
//            Long totalPhysicalMemorySize = jsonObject.getLong("totalPhysicalMemorySize");
//            Long freePhysicalMemorySize = jsonObject.getLong("freePhysicalMemorySize");
//            double totalMemory = 1.0 * totalPhysicalMemorySize / GB;
//            double freeMemory = 1.0 * freePhysicalMemorySize / GB;
//            double memoryUseRatio = 1.0 * (totalPhysicalMemorySize - freePhysicalMemorySize) / totalPhysicalMemorySize * 100;
//
//            StringBuilder result = new StringBuilder();
//            result.append("系统CPU占用率: ")
//                    .append(twoDecimal(systemCpuLoad))
//                    .append("%，内存占用率：")
//                    .append(twoDecimal(memoryUseRatio))
//                    .append("%，系统总内存：")
//                    .append(twoDecimal(totalMemory))
//                    .append("GB，系统剩余内存：")
//                    .append(twoDecimal(freeMemory))
//                    .append("GB，该进程占用CPU：")
//                    .append(twoDecimal(processCpuLoad))
//                    .append("%");
//            System.out.println(result.toString());
//
//            try {
//                Thread.sleep(500);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    public double twoDecimal(double doubleValue) {
//        BigDecimal bigDecimal = new BigDecimal(doubleValue).setScale(2, RoundingMode.HALF_UP);
//        return bigDecimal.doubleValue();
//    }

}
